package com.mobialia.socialmediademo;

import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public class ShareActivity extends Activity {

	public final static String TAG = "ShareActivity";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// No Titlebar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_PROGRESS);

		setContentView(R.layout.share);
	}

	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume()");

	}

	public void onPause() {
		super.onPause();
	}

	public void onShareChoose(View v) {
		String shareText = ((EditText) findViewById(R.id.EditText)).getText().toString();

		Intent intent = new Intent(android.content.Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(android.content.Intent.EXTRA_TEXT, shareText);
		startActivity(Intent.createChooser(intent, getResources().getText(R.string.share_choose)));
	}
	
	public void onShareTwitter(View v) {
		String shareText = ((EditText) findViewById(R.id.EditText)).getText().toString();

		Context context = getApplication();
		try {
			Intent intent = new Intent(Intent.ACTION_SEND);
			intent.putExtra(Intent.EXTRA_TEXT, shareText);
			intent.setType("text/plain");
			PackageManager packageManager = context.getPackageManager();
			List<ResolveInfo> activityList = packageManager.queryIntentActivities(intent, 0);
			for (ResolveInfo act : activityList) {
				Log.d(TAG, act.activityInfo.name);
				if (act.activityInfo.name.indexOf("com.twitter.") == 0) { // Check it if starts by...
					ComponentName name = new ComponentName(act.activityInfo.applicationInfo.packageName, act.activityInfo.name);
					intent.setComponent(name);
					startActivity(intent);
					return;
				}
			}
		} catch (final ActivityNotFoundException e) {}
		Toast.makeText(this, R.string.error_no_twitter_found, Toast.LENGTH_SHORT).show();
	}
}